package com.blog.controller.manage;

import com.blog.bean.FriendChain;
import com.blog.constant.RoleConstant;
import com.blog.service.FriendChainService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/friendChainManage")
public class FriendChainManageController {

    @Resource
    private FriendChainService friendChainService;

    /**
     * 添加友链
     *
     * @param friendChain
     * @return
     */
    @RequestMapping("/addFriendChain")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object addFriendChain(FriendChain friendChain) {
        Map<Object, Object> map = new HashMap<>();
        int count = friendChainService.insertSelective(friendChain);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 更新友链
     *
     * @param friendChain
     * @return
     */
    @RequestMapping("/updateFriendChain")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object updateFriendChain(FriendChain friendChain) {
        Map<Object, Object> map = new HashMap<>();
        int count = friendChainService.updateByPrimaryKeySelective(friendChain);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 查询一个友链
     *
     * @param friendChainId
     * @return
     */
    @RequestMapping("/getFriendChain")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object getFriendChain(@RequestParam(value = "friendChainId") Long friendChainId) {
        Map<Object, Object> map = new HashMap<>();
        FriendChain temp = new FriendChain();
        temp.setFriendChainId(friendChainId);
        FriendChain friendChain = friendChainService.getFriendChain(temp);
        if (friendChain != null) {
            map.put("friendChain", friendChain);
        }
        return map;
    }

    /**
     * 删除友链
     *
     * @param friendChainId
     * @return
     */
    @RequestMapping("/deleteFriendChain")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    @ResponseBody
    public Object deleteFriendChain(@RequestParam(value = "friendChainId") Long friendChainId) {
        Map<Object, Object> map = new HashMap<>();
        int count = friendChainService.deleteByPrimaryKey(friendChainId);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 友链列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/friendChainList")
    @RequiresRoles(RoleConstant.ROLE_ADMIN)
    public String friendChainList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        List<FriendChain> friendChainList = friendChainService.selectFriendChain(new FriendChain());
        PageInfo<FriendChain> pageInfo = new PageInfo<>(friendChainList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-friend-chain";
    }
}
